const express = require('express')
const router = express.Router()

// 模拟数据生成
const generateMockData = (page = 1, pageSize = 10) => {
  const total = 100
  const startIndex = (page - 1) * pageSize
  
  const mockData = []
  const titles = [
    '跨端开发最佳实践分享',
    'Vue3 + uni-app 实战教程',
    '鸿蒙Next应用开发指南',
    '小程序性能优化技巧',
    '快应用开发入门到精通',
    '移动端适配解决方案',
    'TypeScript在跨端开发中的应用',
    '状态管理Pinia使用详解',
    '组件库设计与实现',
    '自动化测试在跨端开发中的实践'
  ]
  
  const tags = ['Vue3', 'uni-app', '跨端', '小程序', '鸿蒙', '性能优化', '实战', '教程']
  
  for (let i = 0; i < pageSize && startIndex + i < total; i++) {
    const index = startIndex + i
    mockData.push({
      id: index + 1,
      title: titles[index % titles.length] + ` #${index + 1}`,
      subtitle: `这是第${index + 1}条数据的副标题`,
      description: `这是一条关于${titles[index % titles.length]}的详细描述内容，包含了丰富的技术细节和实践经验分享。`,
      avatar: `https://picsum.photos/200/200?random=${index + 1}`,
      time: new Date(Date.now() - index * 24 * 60 * 60 * 1000).toLocaleDateString('zh-CN'),
      tags: tags.slice(index % 3, index % 3 + 3)
    })
  }
  
  return {
    list: mockData,
    total,
    page,
    pageSize,
    hasMore: startIndex + pageSize < total
  }
}

// 获取列表数据
router.get('/list', (req, res) => {
  const { page = 1, pageSize = 10 } = req.query
  
  // 模拟网络延迟
  setTimeout(() => {
    const data = generateMockData(parseInt(page), parseInt(pageSize))
    
    res.json({
      code: 200,
      message: 'success',
      data
    })
  }, 500)
})

// 获取用户信息
router.get('/user/info', (req, res) => {
  res.json({
    code: 200,
    message: 'success',
    data: {
      userId: 'cross_platform_001',
      username: '跨端开发者',
      email: 'developer@example.com',
      phone: '13800138000',
      avatar: 'https://picsum.photos/200/200?random=user',
      level: 5,
      points: 8888,
      createTime: '2024-01-01'
    }
  })
})

// 更新用户信息
router.post('/user/update', (req, res) => {
  const { username, email, phone } = req.body
  
  // 模拟验证
  if (!username || username.length < 2) {
    return res.status(400).json({
      code: 400,
      message: '用户名长度不能少于2个字符'
    })
  }
  
  res.json({
    code: 200,
    message: '用户信息更新成功',
    data: {
      username,
      email,
      phone,
      updateTime: new Date().toISOString()
    }
  })
})

// 获取配置信息
router.get('/config', (req, res) => {
  res.json({
    code: 200,
    message: 'success',
    data: {
      appName: '跨端Vue框架',
      version: '1.0.0',
      build: 100,
      updateUrl: 'https://example.com/update',
      features: [
        '跨平台支持',
        '深色模式',
        '离线缓存',
        '推送通知',
        '数据统计'
      ],
      platforms: [
        'iOS',
        'Android',
        '鸿蒙Next',
        'Web',
        '微信小程序',
        '支付宝小程序',
        '百度小程序',
        '抖音小程序',
        '飞书小程序',
        'QQ小程序',
        '快手小程序',
        '钉钉小程序',
        '淘宝小程序',
        '京东小程序',
        '小红书小程序',
        '快应用',
        '鸿蒙元服务'
      ]
    }
  })
})

// 文件上传
router.post('/upload', (req, res) => {
  // 模拟文件上传
  setTimeout(() => {
    res.json({
      code: 200,
      message: '文件上传成功',
      data: {
        url: 'https://picsum.photos/400/400?random=upload',
        filename: 'uploaded_file.jpg',
        size: 102400,
        type: 'image/jpeg'
      }
    })
  }, 1000)
})

// 健康检查
router.get('/health', (req, res) => {
  res.json({
    status: 'healthy',
    timestamp: new Date().toISOString(),
    uptime: process.uptime(),
    memory: process.memoryUsage(),
    version: '1.0.0'
  })
})

module.exports = router
